package com.lianxi.nmms.controller;

import com.github.pagehelper.PageInfo;
import com.lianxi.nmms.constant.ResponseCodeConstant;
import com.lianxi.nmms.entity.Product;
import com.lianxi.nmms.entity.ProductType;
import com.lianxi.nmms.exception.FileUploadErrorException;
import com.lianxi.nmms.exception.ProductTypeExistException;
import com.lianxi.nmms.exception.RequestParameterErrorException;
import com.lianxi.nmms.service.ProductService;
import com.lianxi.nmms.service.ProductTypeService;
import com.lianxi.nmms.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 翔哥
 * 2025年09月29日
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductTypeService typeService;

    @Autowired
    private ProductService proService;

    @RequestMapping("/findAll")
    public ModelAndView findAll(String pageNo, String pageSize) {
        ModelAndView mav = new ModelAndView();
        PageInfo<Product> proPage = proService.findPage(pageNo, pageSize);

//            proService.findType(proPage);
        mav.addObject("proPage", proPage);
        List<ProductType> allTypes = typeService.findAll();
        List<ProductType> notTypes = typeService.findStatus();
        mav.addObject("allTypes", allTypes);
        mav.addObject("notTypes", notTypes);
        mav.setViewName("backend/productManager");


        return mav;
    }

    @RequestMapping("/addProduct")
    public ResponseResult addProduct(String name, String price, String description, @RequestParam("file") CommonsMultipartFile file, String typeId, HttpSession session) {
        ResponseResult result = new ResponseResult();
        try {
            proService.addProduct(name, price, description, file, typeId, session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/findProductById")
    public ResponseResult findProductById(String id) {
        ResponseResult result = new ResponseResult();
        try {
            Product product = proService.findProductById(id);
            result.setMessage("成功");
            result.setReturnObject(product);
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (Exception e) {
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/updateProduct")
    public ResponseResult updateProduct(String id,String name, String price, String description, @RequestParam("file") CommonsMultipartFile file, String typeId, HttpSession session) {
        ResponseResult result = new ResponseResult();
        try {
            System.out.println("id是*******************************" + id);
            proService.updateProduct(id,name, price, description, file, typeId, session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (RequestParameterErrorException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_REQUEST_PARAMETER_ERROR);
        } catch (ProductTypeExistException e) {
            result.setMessage(e.getMessage());
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }

    @RequestMapping("/deleteProduct")
    public ResponseResult deleteProduct(String id, HttpSession session) {
        ResponseResult result = new ResponseResult();
        try {
            proService.deleteProduct(id, session);
            result.setMessage("成功");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("服务器内部异常");
            result.setResponseCode(ResponseCodeConstant.RESPONSE_CODE_FAIL);
        }

        return result;
    }
}
